package games.tanchishe;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class GamePanel extends JPanel implements ActionListener, KeyListener {
    // 方向，默认向右
    private String direction = "R";
    // 定时器，每次触发重新绘制图形
    private Timer timer = new Timer(100, this);

    public GamePanel() {
        this.setFocusable(true);
        this.addKeyListener(this);
        timer.start();
    }



    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // 背景色填充
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, this.getSize().width , this.getSize().height);
        // 绘制网格
        drawLine(g);
        // 文字提示
        if (!Data.isGameStart) {
            if (Data.isDead) {
                g.setColor(Color.RED);
                g.setFont(new Font("微软雅黑", Font.BOLD, 20));
                g.drawString("游戏结束，按空格重新开始", 300, 700);
            } else {
                g.setColor(Color.white);
                g.setFont(new Font("微软雅黑", Font.BOLD, 20));
                g.drawString("按下空格开始或暂停，通过WASD控制方向", 300, 700);
            }
        } else {
            g.setColor(Color.BLACK);
            g.fillRect(100, 300, 500, 50);
            drawLine(g);
        }

        // 绘制snake
        for (Snake snake : Data.snakeList) {
            g.setColor(snake.getColor());
            for (int i = 0; i < snake.getLength(); i++) {
                g.setColor(snake.getColor(i));
                g.fillOval(snake.getBodyX()[i], snake.getBodyY()[i], snake.getWidth(), snake.getWidth());
            }
        }
        // 绘制food
        if (Data.food != null) {
            g.setColor(Data.food.getColor());
            g.fillOval(Data.food.getX(), Data.food.getY(), Data.food.getWidth(), Data.food.getWidth());
        }

    }

    private void drawLine(Graphics g) {
        g.setColor(Color.darkGray);
        for (int i = 0; i < 100; i++) {
            g.drawLine(0, i*10, 1000, i*10);
            g.drawLine(i*10, 0, i*10, 1000);
        }
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (Data.isGameStart) {
            // 每一帧向指定方向移动一个位置
            for (Snake snake : Data.snakeList) {
                // 移动
                move(snake);
            }
            // 重新绘制面板
            repaint();
        }
        timer.start();
    }

    private void move(Snake snake) {
        //
        for (int i = snake.getLength() - 1; i > 0; i--) {
            snake.getBodyX()[i] = snake.getBodyX()[i-1];
            snake.getBodyY()[i] = snake.getBodyY()[i-1];
        }
        // 头部位置
        if ("R".equals(direction)){
            if (snake.getBodyY()[0] != snake.getBodyY()[1]) {

            }
            snake.getBodyX()[0] = snake.getBodyX()[0] + snake.getWidth();
            if (snake.getBodyX()[0] > this.getSize().getWidth()) {
                snake.getBodyX()[0] = 0;
            }
        } else if ("L".equals(direction)){
            // 不能原路返回
            if (snake.getBodyY()[0] != snake.getBodyY()[1]) {

            }
            snake.getBodyX()[0] = snake.getBodyX()[0] - snake.getWidth();
            if (snake.getBodyX()[0] <= 0) {
                snake.getBodyX()[0] = this.getSize().width;
            }
        } else if ("U".equals(direction)){
            snake.getBodyY()[0] = snake.getBodyY()[0] - snake.getWidth();
            if (snake.getBodyY()[0] <= 0) {
                snake.getBodyY()[0] = this.getSize().height;
            }
        } else if ("D".equals(direction)){
            snake.getBodyY()[0] = snake.getBodyY()[0] + snake.getWidth();
            if (snake.getBodyY()[0] >= this.getSize().height) {
                snake.getBodyY()[0] = snake.getWidth();
            }
        }
        boolean dead = snake.isDead();
        Data.isDead = dead;
        if (dead) {
            System.out.println("死了");
            Data.isGameStart = false;
            // 重新初始化蛇的长度
            snake.init();
        }
        boolean eat = snake.eat(Data.food);
        if (eat) {
            createFood();
        }

    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        System.out.println("按下键：" + e.getKeyChar());
        if (keyCode == KeyEvent.VK_SPACE) {
            // 按下空格开始或暂停
            Data.isGameStart = !Data.isGameStart;
            Data.isDead = false;
        }
        // 改变移动方向
        switch (keyCode) {
            case KeyEvent.VK_W: if (!"D".equals(direction)){direction = "U";}; break;
            case KeyEvent.VK_A: if (!"R".equals(direction)){direction = "L";}; break;
            case KeyEvent.VK_S: if (!"U".equals(direction)){direction = "D";}; break;
            case KeyEvent.VK_D: if (!"L".equals(direction)){direction = "R";}; break;
        }
        // 重新渲染
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    public void createFood() {
        Random random = new Random();
        // 每个格子大小
        int rate = 10;
        int x = random.nextInt(40) * rate;
        // 纵坐标最多有几个
        int y_max_count = this.getSize().height / rate;
        int y = random.nextInt(y_max_count - 1) * rate;
        Color color = Data.colors[random.nextInt(Data.colors.length)];
        Food food = new Food(x, y, rate, color);
        Data.food = food;
    }
}
